package com.ning.parking.config;

import cn.hutool.core.util.ObjectUtil;
import com.ning.parking.domain.ParkingUserInfo;
import com.scsonic.framework.common.constant.SecurityConstants;
import com.scsonic.framework.exception.TokenException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author peng can
 * @date 2023/7/13
 */
@Slf4j
public class AuthInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        String accessToken = GlobalVariableUtils.getToken(request);

        if (ObjectUtil.isEmpty(accessToken)) {
            throw new TokenException("非法请求");
        }

        ParkingUserInfo user = GlobalVariableUtils.getUser(accessToken);

        if (ObjectUtil.isEmpty(user)) {
            throw new TokenException("用户token已失效，请重新登陆！");
        }
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }



}
